package com.easybacklog.client.webservices.subversion;

import java.util.ArrayList;

import com.easybacklog.client.EasyBacklogGlobalVariables;
import com.easybacklog.client.model.ProjectClass;
import com.easybacklog.client.model.ProjectFeature;
import com.easybacklog.client.sourcefeatures.SourceFeaturesList;
import com.easybacklog.client.sourcefeatures.SourceFeaturesView;
import com.easybacklog.client.utilities.FeatureNameGenerator;
import com.easybacklog.client.utilities.FileExtensions;
import com.easybacklog.client.webservices.ConvertJsonp;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class RequestGetSubversionSpecificationFile {

	// http://livesourceagile.com:8080/LiveSourceWebServices/GetSubversionSpecificationFileService?
	// repositoryURL=https://tictactoe4j.googlecode.com/svn/trunk
	// &username=alline.oliveira&password=bD8Km8se2rV9
	// &projectID=tictactoe4j

	private static String url = "http://livesourceagile.com:8080/LiveSourceWebServices/GetSubversionSpecificationFileService";

	public static void loadJSONFile() {

		// String username = EncryptText.decrypt(GetCookies.getUsername(project
		// .getProjectID()));
		String username = null;

		// String password =
		// EncryptText.decrypt(GetCookies.getPassword(project.getProjectID()));
		String password = null;

		JsonpRequestBuilder jsonp = new JsonpRequestBuilder();

		jsonp.setTimeout(50000);

		String urlSave = url + "?repositoryURL="
				+ EasyBacklogGlobalVariables.currentProject.getRepositoryURL()
				+ "&projectID="
				+ EasyBacklogGlobalVariables.currentProject.getProjectID()
				+ "&username=" + username + "&password=" + password;

		jsonp.requestObject(urlSave, new AsyncCallback<JavaScriptObject>() {

			public void onFailure(Throwable throwable) {

				System.out.println("Error: " + throwable);
				Window.alert(throwable.toString());
			}

			public void onSuccess(JavaScriptObject feed) {

				JSONObject objFeed = new JSONObject(feed);

				ArrayList<ProjectClass> sourceList = new ArrayList<ProjectClass>();

				for (int i = 0; i < objFeed.size(); i++) {

					JSONObject entry = (JSONObject) objFeed.get(i + "");

					ProjectClass projectClass = new ProjectClass();

					String fileExtension = FileExtensions
							.getFileExtension(ConvertJsonp
									.convertToString(entry.get("fileName")));

					if (entry.get("class_tags") != null
							&& FileExtensions.EXTENSION_JAVA
									.equals(fileExtension)) {

						projectClass
								.setFeatureType(ConvertJsonpToJavaDocumentation
										.getClassType(entry.get("class_tags")
												.isArray()));

						if (ProjectFeature.FEATURE_TYPE.equals(projectClass
								.getFeatureType())) {

							projectClass = ConvertJsonpToJavaDocumentation
									.convert(projectClass, entry);

							projectClass
									.setStatus(ConvertJsonpToJavaDocumentation
											.getStatus(entry.get("class_tags")
													.isArray()));

							projectClass.setFilePath(ConvertJsonp
									.convertToString(entry.get("filePath")));

							projectClass.setFileName(ConvertJsonp
									.convertToString(entry.get("fileName")));

							projectClass.setFeaturePath(FeatureNameGenerator
									.getLastNameSpaced(
											projectClass.getFeaturePath(), "/"));

							projectClass.setFeatureName(FeatureNameGenerator
									.getFileSpacedName(projectClass
											.getFileName()));

							sourceList.add(projectClass);
						}
					}
				}

				EasyBacklogGlobalVariables.sourceFeatures = sourceList;

				SourceFeaturesView.vpSourceFeatures.clear();

				SourceFeaturesView.vpSourceFeatures
						.add(new SourceFeaturesList());
			}
		});
	}
}
